
from django.contrib.auth.decorators import login_required





from django import http

from ihome.utils.response_code import RET
from users.models import User


# 登录验证，房东验证
def landlord_login_required(view):
    def wrapper(request,*args, **kwargs):
        user = request.user
        if not request.user.is_authenticated:
            return http.JsonResponse({'errmsg' : '未登录' ,
                                      'errno' : RET.SESSIONERR})
        if not all([user.id_card,user.real_name,user.avatar_url]):
            return http.JsonResponse({'errmsg': '不是房东，无法新增房源',
                                      'errno': RET.ROLEERR})

        return view(request,*args, **kwargs)

    return wrapper

class LandlordMixin(object):

    @classmethod
    def as_view(cls, *args, **kwargs):
        view = super().as_view(*args, **kwargs)


        return landlord_login_required(view)




# 登录验证
def landlord(view):
    def wrapper(request,*args, **kwargs):
        user = request.user
        if not request.user.is_authenticated:
            return http.JsonResponse({'errmsg' : '未登录' ,
                                      'errno' : RET.SESSIONERR})



        return view(request,*args, **kwargs)

    return wrapper

class LoginRequiredMixin(object):

    @classmethod
    def as_view(cls, *args, **kwargs):
        view = super().as_view(*args, **kwargs)


        return landlord(view)